package org.thesis;


import org.thesis.activity.ListMailActivity;

import android.app.Activity;
import uit.mmt02.R;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;

public class Esafe2Activity extends Activity 
{
	org.thesis.doituong.showToast st; //thong bao
	MediaPlayer mp;  //choi nhac 
	SharedPreferences sp ; //dem so lan chay chuong trinh, check dau login
	
	final static String DB_SHAREPRE = "db_checkFirstLogin";
	final static String FLAGCHECK = "flagCheck";
	final static String COUNT_ESAFE = "count_Esafe"; // bien luu so lan chay chuong trinh
	int solan_chay_chuongtrinh ;
	String tag  = "Esafe2Activity";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        startMusic();
        st = new org.thesis.doituong.showToast(getBaseContext());
        

        
        // tao SharePreferrence
       
        sp = getSharedPreferences(DB_SHAREPRE,MODE_WORLD_WRITEABLE);
        SharedPreferences.Editor spEditor = sp.edit();
        // dem so lan chay chuong trinh
        solan_chay_chuongtrinh = sp.getInt(COUNT_ESAFE, 1);
        Log.v(tag, "Esafe duoc chay lan  : " + solan_chay_chuongtrinh);
        spEditor.putInt(COUNT_ESAFE, (solan_chay_chuongtrinh+1));
        spEditor.commit();
        
//        Boolean check_firstLogin = sp.getBoolean(FLAGCHECK, true);
        Boolean check_firstLogin = false;
        
        //kiem tra csdl
        org.thesis.dataAccess.DB_EmailLoginManager_Adapter mydb = new org.thesis.dataAccess.DB_EmailLoginManager_Adapter(this);
        mydb.open();
        Cursor mCursor = mydb.get_email_tongmail_newmail();
        if(mCursor.getCount() == 0) // neu CSDL trong
        	check_firstLogin = true;
        mCursor.close();
        mydb.close();
        
        //lay thong tin co check first login
//        if(sp.getBoolean(FLAGCHECK, true) == true) // lan dau login,
        if(check_firstLogin) // lan dau login,
        {
        	//goi Activity Introduce
        	Intent i = new Intent(this,org.thesis.activity.IntroduceActivity.class);
			startActivity(i);
			//set la co la flase // da qua lan dau chay chuong trinh
//			spEditor.putBoolean(FLAGCHECK, false);
//			spEditor.commit();
        }
        else
        {
        	Intent i = new Intent(this,org.thesis.activity.ListMailActivity.class);
			startActivity(i);
        }
    }
    public void img_onClick(View v)
    {
    	ImageButton img = (ImageButton)v;
    	switch (img.getId()) {
		case R.id.img_main_next:
			startActivity(new Intent(this, ListMailActivity.class));
			break;
		default:
			break;
		}
    }
    
    public void startMusic()
    {
    	mp = MediaPlayer.create(getBaseContext(), R.raw.btn_click);
    	mp.start();
    }
}